'''
copyright: Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
Author: YangYunYi
Date: 2022-03-23 17:04:25
LastEditTime: 2022-03-23 17:24:05
LastEditors: YangYunYi
Description: 
FilePath: \li-wen-master\getdata.py
symbol_custom_string_obkoro1: 可以输入预定的版权声明、个性签名、空行等
'''
import json
import time
import hashlib
import os
import csv
from main.monitor.workerstatus import QueryOBSWorker
from get_data_list import MetricsHandler

host_list = ["172.16.1.99", "172.16.1.68", "172.16.1.51", "172.16.1.96", "172.16.1.27", "172.16.1.12", "172.16.1.14",
             "172.16.1.182", "172.168.1.5", "172.168.1.6", "172.168.1.7", "172.168.1.8", "172.168.1.45", "172.168.1.46",
             "172.168.1.47", "172.168.1.48",
             "172.168.1.124", "172.168.1.81", "172.168.1.84", "172.168.1.89", "172.168.1.95",
             "172.168.1.158", "172.168.1.194", "172.168.1.129", "172.168.1.91", "172.168.1.163", "172.168.1.3",
             "172.168.1.189"]
data_index = {}
query_obs_worker = QueryOBSWorker()


class DataGate:
    def __init__(self):
        self.data_index = {}


    def get_data_list(self):
        with open("data_list.json", "r") as data_list:
            json_data = json.load(data_list)
            return json_data


    def save_csv(self, data_name, values, host_name):
        host_dir = os.path.join("data", host_name)
        if not os.path.exists(host_dir):
            os.makedirs(host_dir)
        data_name_hash = hashlib.md5(data_name.encode('utf-8')).hexdigest()
        self.data_index[data_name_hash] = data_name
        file_name = os.path.join(host_dir, str(data_name_hash) +".csv")
        with open(file_name, 'w') as csv_file:
            writer = csv.writer(csv_file)
            for val in values:
                timeArray = time.localtime(val[0])
                otherStyleTime = time.strftime("%d-%b-%y %I.%M.%S %p", timeArray)
                writer.writerow([otherStyleTime, val[1]])
        index_file_path = os.path.join(host_dir, "data_index.json")
        with open(index_file_path, 'w') as index_file:
            json.dump(data_index, index_file)


    def get_data_from_obs_workers(self, host_ip, time_range):
        metrics_handler = MetricsHandler()
        metrics_handler.get_metrics(host_ip)
        data_list = metrics_handler.data_list
        ret = self.get_data(host_ip, data_list, time_range)


    # def get_data_from_obs_workers(time_range):
    #     #query_obs_worker = QueryOBSWorker()
    #     obs_workers_ip = query_obs_worker.get_all_worker_list()
    #     print(obs_workers_ip)
    #     data_list = get_data_list()
    #     ret = get_data("172.16.1.129", data_list, time_range)
    # #     for arch, arch_ip_list in obs_workers_ip.items():
    # #         for ip in arch_ip_list:
    # #             ret = get_data(ip, data_list, time_range)
    # #             #print(ret)

    def get_data(self, host_ip, data_list, time_range):
        for data in data_list["gauge"]:
            query_str = query_obs_worker.query_add_host(data, host_ip)
            ret = query_obs_worker.get_aops_resource(query_str, time_range)
            #    print(ret)
            if ret:
                # print(ret)
                self.save_csv(data, ret, host_ip)

        for data in data_list["counter"]:
            query_str = query_obs_worker.query_add_host(data, host_ip)
            query_str = query_obs_worker.query_rate(query_str)
            ret = query_obs_worker.get_aops_resource(query_str, time_range)
            # print(ret)
            if ret:
                # print(ret)
                self.save_csv(data, ret, host_ip)

    def get_data_index(self, host_ip):
        data_index = {}
        metrics_handler = MetricsHandler()
        metrics_handler.get_metrics(host_ip)
        data_list = metrics_handler.data_list
        for data_name in data_list["gauge"]:
            data_name_hash = hashlib.md5(data_name.encode('utf-8')).hexdigest()
            data_index[data_name_hash] = data_name
        for data_name in data_list["counter"]:
            data_name_hash = hashlib.md5(data_name.encode('utf-8')).hexdigest()
            data_index[data_name_hash] = data_name
        if not os.path.exists("data_index"):
            os.mkdir("data_index")
        index_file_path = os.path.join("data_index", host_ip + "_data_index.json")
        with open(index_file_path, 'w') as index_file:
            json.dump(data_index, index_file)


def main():
    time_range = (1647100967, 1648007532)
    for host_ip in host_list:
        data_gate = DataGate()
        #data_gate.get_data_from_obs_workers(host_ip, time_range)
        data_gate.get_data_index(host_ip)
    # with open("data_index.json", "w") as data_index_file:
    #     json.dump(data_index, data_index_file)


if __name__ == "__main__":
    main()
